﻿using System;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using LMD.Lib.Oracle;
using CoreLab.Oracle;
using System.Collections.Generic;

public class clsSearchMAP
    {
        public clsSearchMAP()
        { }

        public clsSearchMAP(SearchType WhichType, string SourceURL, string SearchText,
            string FieldToSearch, string FoundedObject)
        {
            this.pFieldToSearch = FieldToSearch;
            this.pFoundedObject = FoundedObject;
            this.pSearchText = SearchText;
            this.pSourceURL = SourceURL;
        }

        public void Clear()
        {
            this.pFieldToSearch = "";
            this.pFoundedObject = "";
            this.pSearchText = "";
            this.pSourceURL = "";
        }

        private SearchType pSearchingType;
        public SearchType SearchingType
        {
            get { return pSearchingType; }
            set { pSearchingType = value; }
        }

        private string pSourceURL;
        [DefaultSettingValue("")]
        public string SourceURL
        {
            get { return pSourceURL; }
            set { pSourceURL = value; }
        }

        private string pSearchText;
        [DefaultSettingValue("")]
        public string SearchText
        {
            get { return pSearchText; }
            set { pSearchText = value; }
        }

        private string pFieldToSearch;
        [DefaultSettingValue("")]
        public string FieldToSearch
        {
            get { return pFieldToSearch; }
            set { pFieldToSearch = value; }
        }

        private object pFoundedObject;
        [DefaultSettingValue(null)]
        public object FoundedObject
        {
            get { return pFoundedObject; }
            set { pFoundedObject = value; }
        }

    }

    public class clsSearchResultMAP
    {
        public clsSearchResultMAP()
        { }

        public clsSearchResultMAP(string SearchCode, string SearchValue)
        {
            this.pSearchCode = SearchCode;
            this.pSearchValue = SearchValue;
        }

        private string pSearchCode;
        [DefaultSettingValue("")]
        public string SearchCode
        {
            get { return pSearchCode; }
            set { pSearchCode = value; }
        }

        private string pSearchValue;
        [DefaultSettingValue("")]
        public string SearchValue
        {
            get { return pSearchValue; }
            set { pSearchValue = value; }
        }

        private string pSearchValueExtension;
        [DefaultSettingValue("")]
        public string SearchValueExtension
        {
            get { return pSearchValueExtension; }
            set { pSearchValueExtension = value; }
        }
    }

    public class clsSearchSQL
    {
        public clsSearchSQL() { }


        public ArrayList GetAllData(SearchType pView, string pSearch, string pCondition, ReturnType pCodeOnly)
        {
            ArrayList oList = new ArrayList();
            DBLib dbl = new DBLib();
            DataTable dt;
            string pViewName = "V_" + pView.ToString();

            string pSearchCondition = "";

            switch (pCodeOnly)
            {
                case ReturnType.CodeOnly:
                    pSearchCondition = String.Format("SearchCode like '%{0}%'", pSearch.Replace(" ", "%"));
                    break;
                case ReturnType.ValueOnly:
                    pSearchCondition = String.Format("SearchValue like '%{0}%'", pSearch.Replace(" ", "%"));
                    break;
                default:
                    pSearchCondition = String.Format("((SearchCode like '%{0}%'", pSearch.Substring(0, pSearch.IndexOf('-') >= 0 ? pSearch.IndexOf('-') : pSearch.Length).Replace(" ", "%"));
                    pSearchCondition += String.Format(" and SearchValue like '%{0}%')", pSearch.Substring(pSearch.IndexOf('-') >= 0 ? pSearch.IndexOf('-') + 1 : 0, pSearch.IndexOf('-') >= 0 ? pSearch.Length - (pSearch.IndexOf('-') + 1) : 0).Replace(" ", "%"));
                    pSearchCondition += String.Format(" or SearchValue like '%{0}%')", pSearch.Substring(0, pSearch.IndexOf('-') >= 0 ? pSearch.IndexOf('-') : pSearch.Length).Replace(" ", "%"));
                    break;
            }
            //pSearchCondition += pCondition;

            String sxQuery = String.Format(@"select * from {0} where {1}", pViewName, pSearchCondition);

            dt = dbl.GetDataTable(sxQuery);
            foreach (DataRow dr in dt.Rows)
            {
                oList.Add(new clsSearchResultMAP(dr["searchcode"].ToString(), dr["searchvalue"].ToString()));
            }

            return oList;
        }
    }
    
    public enum SearchType
    {
        SearchContact
    }

    public enum ReturnType
    {
        ValueOnly,
        CodeOnly,
        CodeValue
    }

